package com.crp.unitalinforbaseplat.app.unitalinforbaseplat.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.crp.unitalinforbaseplat.app.core.BaseController;
import com.crp.unitalinforbaseplat.app.core.Message;
import com.crp.unitalinforbaseplat.app.core.MessageUtil;
import com.crp.unitalinforbaseplat.app.core.utils.CloneUtil;
import com.crp.unitalinforbaseplat.app.core.utils.EmptyUtils;
import com.crp.unitalinforbaseplat.app.unitalinforbaseplat.entity.*;
import com.crp.unitalinforbaseplat.app.unitalinforbaseplat.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 库存调换单 前端控制器
 * </p>
 *
 * @author zsx
 * @since 2020-04-20
 */
@RestController
@RequestMapping("/storeExchange")
public class StoreExchangeController extends BaseController {

    @Autowired
    IStoreExchangeService storeExchangeService;
    @Autowired
    IPartsService partsService;
    @Autowired
    IStoreService storeService;
    @Autowired
    IEnterStoreService enterStoreService;
    @Autowired
    IExitStoreService exitStoreService;
    @RequestMapping("/list_index")
    ModelAndView index() {
        ModelAndView modelAndView = new ModelAndView("backend/unitalinforbaseplat/storeExchange/list");
        modelAndView.addObject("roleName",getRoleName());
        return modelAndView;
    }
    @RequestMapping("/add_index")
    ModelAndView addIndex(StoreExchange storeExchange) {
        ModelAndView modelAndView = new ModelAndView("backend/unitalinforbaseplat/storeExchange/add");
        modelAndView.addObject("roleName",getRoleName());
        if(!EmptyUtils.isEmpty(storeExchange.getId())){
            modelAndView.addObject("storeExchange",storeExchangeService.getById(storeExchange.getId()));
        }else{
            modelAndView.addObject("storeExchange",storeExchange);
        }
        return modelAndView;
    }

    @RequestMapping("/review_index")
    ModelAndView review_index(StoreExchange storeExchange) {
        ModelAndView modelAndView = new ModelAndView("backend/unitalinforbaseplat/storeExchange/review");
        modelAndView.addObject("roleName",getRoleName());
        modelAndView.addObject("storeExchange",storeExchangeService.getById(storeExchange));
        return modelAndView;
    }

    @ResponseBody
    @RequestMapping("/list")
    PageBean<StoreExchange> index(StoreExchange storeExchange) {
        PageBean<StoreExchange> pageBean = new PageBean<StoreExchange>();
        List<StoreExchange> storeList = storeExchangeService.list(new LambdaQueryWrapper<StoreExchange>().like(StringUtils.isNotBlank(storeExchange.getName()),StoreExchange::getName,storeExchange.getName()));
        pageBean.setData(storeList);
        return pageBean;
    }


    @ResponseBody
    @RequestMapping("/delete")
    Message delete(StoreExchange storeExchange) {
        storeExchangeService.removeById(storeExchange.getId());
        return MessageUtil.createMessage(true,"删除成功");
    }

    @ResponseBody
    @RequestMapping("/add")
    Message add(StoreExchange storeExchange) {
        storeExchange.setCreateUserId(getId().intValue());
        storeExchange.setCreateTime(new Date());
        Parts parts = partsService.getById(storeExchange.getPartsId());
        Store store = storeService.getById(storeExchange.getOriginStoreId());
        Store store1 = storeService.getById(storeExchange.getAimStoreId());
        storeExchange.setName(parts.getName());
        storeExchange.setSpecifications(parts.getSpecifications());
        storeExchange.setModel(parts.getModel());
        storeExchange.setManufacturer(parts.getManufacturer());
        storeExchange.setOriginStoreName(store.getName());
        storeExchange.setAimStoreName(store1.getName());
        storeExchange.setApplicationName(getUserName());
        storeExchange.setStatus(1);
        List<EnterStore> enterStoreList = enterStoreService.list(new LambdaQueryWrapper<EnterStore>().eq(EnterStore::getPartsId,storeExchange.getPartsId()).eq(EnterStore::getStoreId,storeExchange.getOriginStoreId()));
        List<ExitStore> exitStoreList = exitStoreService.list(new LambdaQueryWrapper<ExitStore>().eq(ExitStore::getPartsId,storeExchange.getPartsId()).eq(ExitStore::getStoreId,storeExchange.getOriginStoreId()));
        Integer sum = 0;
        if(!EmptyUtils.isAllEmpty(enterStoreList)){
            for (EnterStore enterStore : enterStoreList) {
                sum += enterStore.getRemainNum();
            }
        }
//        if(!EmptyUtils.isAllEmpty(exitStoreList)){
//            for (ExitStore exitStore : exitStoreList) {
//                sum -= exitStore.getNum();
//            }
//        }
//        if(sum <= 0){
//            return MessageUtil.createMessage(false,"无可调换的配件");
//        }
        if(EmptyUtils.isEmpty(storeExchange.getId())){
            if(storeExchangeService.save(storeExchange)){
                return MessageUtil.createMessage(true,"新增成功");
            }else {
                return MessageUtil.createMessage(false,"新增失败");
            }
        }else {
            if(storeExchangeService.updateById(storeExchange)){
                return MessageUtil.createMessage(true,"修改成功");
            }else {
                return MessageUtil.createMessage(false,"修改失败");
            }
        }
    }

    @ResponseBody
    @RequestMapping("/review")
    Message review(@RequestParam("id")Integer id, @RequestParam("verifierOpinion")String verifierOpinion, @RequestParam("managersOpinion")String managersOpinion, @RequestParam("flag")Boolean flag) {
        StoreExchange result = storeExchangeService.getById(id);
        if(result.getStatus() == 1){
            result.setVerifierOpinion(verifierOpinion);
            if(!flag){
                result.setStatus(4);
                return MessageUtil.createMessage(true,"审核驳回成功");
            }
            result.setStatus(2);
            storeExchangeService.updateById(result);
            return MessageUtil.createMessage(true,"校验员审核通过");
        }else if(result.getStatus() == 2){
            result.setManagersOpinion(managersOpinion);
            if(!flag){
                result.setStatus(4);
                return MessageUtil.createMessage(true,"审核驳回成功");
            }
            result.setStatus(3);
            storeExchangeService.updateById(result);
            // 来源仓库生成一条出库记录  目的仓库生成一条入库记录  状态均为已完成
            // 根据仓库id 和配件id  查询所有入库记录和出库记录
            List<EnterStore> enterStoreList = enterStoreService.list(new LambdaQueryWrapper<EnterStore>().eq(EnterStore::getPartsId,result.getPartsId()).eq(EnterStore::getStoreId,result.getOriginStoreId()));
            List<ExitStore> exitStoreList = exitStoreService.list(new LambdaQueryWrapper<ExitStore>().eq(ExitStore::getPartsId,result.getPartsId()).eq(ExitStore::getStoreId,result.getOriginStoreId()));
            Integer sum = 0;
            if(!EmptyUtils.isAllEmpty(enterStoreList)){
                for (EnterStore enterStore : enterStoreList) {
                    try {
                        EnterStore cloneEn = CloneUtil.clone(enterStore);
                        enterStore.setRemainNum(0);
                        sum += cloneEn.getRemainNum();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
                enterStoreService.updateBatchById(enterStoreList);
            }
//            if(!EmptyUtils.isAllEmpty(exitStoreList)){
//                for (ExitStore exitStore : exitStoreList) {
//                    sum -= exitStore.getNum();
//                }
//            }
//            if(sum <= 0){
//                return MessageUtil.createMessage(false,"无可调换的配件");
//            }
            ExitStore exitStore = new ExitStore();
            exitStore.setCreateUserId(getId().intValue());
            exitStore.setCreateTime(new Date());
            exitStore.setEnterTime(new Date());
            exitStore.setName(result.getName());
            exitStore.setSpecifications(result.getSpecifications());
            exitStore.setModel(result.getModel());
            exitStore.setManufacturer(result.getManufacturer());
            exitStore.setNum(sum);
            exitStore.setStoreId(result.getOriginStoreId());
            exitStore.setStoreName(result.getOriginStoreName());
            exitStore.setApplicationName(result.getApplicationName());
            exitStore.setStatus(3);
            exitStore.setType(1);
            exitStore.setPartsId(result.getPartsId());
            exitStoreService.save(exitStore);
            EnterStore enterStore = new EnterStore();
            enterStore.setCreateUserId(getId().intValue());
            enterStore.setCreateTime(new Date());
            enterStore.setEnterTime(new Date());
            enterStore.setName(result.getName());
            enterStore.setSpecifications(result.getSpecifications());
            enterStore.setModel(result.getModel());
            enterStore.setManufacturer(result.getManufacturer());
            enterStore.setNum(sum);
            enterStore.setRemainNum(sum);
            enterStore.setStoreId(result.getAimStoreId());
            enterStore.setStoreName(result.getAimStoreName());
            enterStore.setApplicationName(result.getApplicationName());
            enterStore.setStatus(3);
            enterStore.setPartsId(result.getPartsId());
            enterStoreService.save(enterStore);
            return MessageUtil.createMessage(true,"经理审核通过");
        }else {
            return MessageUtil.createMessage(false,"非审核状态无法审核");
        }
    }

}
